<?php

namespace app\index\service;

use think\Db;

/**
 * @author xjy
 */
class StoreskuService
{
    private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
    private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期
    private static $salesDistributed=["1"=>"爆品","2"=>"畅销款","3"=>"平销款","4"=>"滞销款","5"=>"无动销"];  //生命周期
    //SKU生命周期列表
    static public function getList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
             $titles=[
                 'shop'=>'店铺','sku'=>'SKU','sales_state_name'=>'销售状态','cycle_stage_name'=>'生命周期',
                 'sku_grade'=>'SKU等级','state'=>'状态'];
        }
        
        return \app\index\model\ProduceModel::getStoreSkuList($param,$titles,"app\index\service\StoreskuService::convertData");  
    }
    public static function convertData(){
       
        $gets=func_get_args();
        if($gets[0]){
            foreach($gets[0] as $key=>&$value){
                $value['sales_state_name']=isset(self::$salesState[$value['sales_state']]) ? self::$salesState[$value['sales_state']]:'';
                $value['cycle_stage_name']=isset(self::$cycleStage[$value['cycle_stage']])? self::$cycleStage[$value['cycle_stage']]:'';
                $value['state']=($value['status']==1) ? "正常":"禁止";
            }
        }
        return $gets[0];  
    }
    //校验批量处理
    static public function uploadHandle($data,$param){
        $msg=[];//提示信息
        $newData=[];
        $num=0;
        $sku=[];$insertData=[];
        $getShop=\app\common\ConfigBase::getShop();
        
        foreach ($data as $k=>$v){
            if(isset($v['1']) && !empty($v['1'])){
                $sku[]=strtoupper($v[1]);
                $newData[]=$v;
                $num++;
            } 
        }
        if($num>5000){
             return ["code" => "0", "msg" => '上传不能大于5000'];
        }
        $res=\app\common\service\SkuService::getBatchInfoBySku($sku);
        $skuName=array_column($res,'cnname','sku');
        foreach($newData as $key=>$value){
            $one=[];$str='';$str1='';$str2='';
            $line=$key+2;
            $cname = isset($skuName[$value['1']]) ? $skuName[$value['1']]:'';
            if(empty($value['0'])  || (!empty($value[0]) && !in_array($value[0],$getShop)) || empty($value['1']) || empty($cname)){
                if(empty($value['0']) || empty($value['1'])){
                    $str="店铺为空或者SKU为空";
                }
                if(empty($cname)){
                    $str1="SKU:{$value['1']}在sku基础资料中不存在";
                }
                if(!in_array($value[0],$getShop)){
                     $str2="店铺:{$value['0']}不存在";
                }
                $msg[]="第{$line}行".$str.",".$str1.",".$str2;
            }
            if(!empty($value['1'])){
                 $one['sku'] = strtoupper($value['1']);
            }
            $one['cname'] = $cname;
            if(!empty($value['0'])){
                 $one['shop'] = $value['0'];
            }
            if(!empty($value['2'])){
                if($value['2']< 1 || $value['2']>4){
                     $msg[]="第{$line}行:销售状态应该在1-4之间";
                }
                 $one['sales_state'] = $value['2'];
            }
            if(!empty($value['3'])){
                if($value['3']< 1 || $value['3']>4){
                     $msg[]="第{$line}行:生命周期应该在1-4之间";
                }
                 $one['cycle_stage'] = $value['3'];
            }
            if(!empty($value['4'])){
                 $one['sku_grade'] = $value['4'];
            }
            $one['update_time'] = date('Y-m-d H:i:s');
            $one['operator'] = CN_NAME;
            if(count($msg)==0){
                $insertData[]=$one;
            }
        }
        if($insertData){
             \app\common\model\ToolModel::dbInsertOrUpdate('store_sku',$insertData);
        }
        return ["code" => "1", "msg" => join('<br/>',$msg)];
    }
}
